package ui;

import dao.TeacherDao;
import model.Teacher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import util.AccountMemoryUtil;

public class TeacherLoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField captchaField;
    private JLabel captchaLabel;
    private String generatedCaptcha;

    public TeacherLoginFrame() {
        setTitle("教师登录");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(8, 10, 8, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 用户名
        gbc.gridx = 0; gbc.gridy = 0;
        add(new JLabel("用户名："), gbc);
        usernameField = new JTextField(15);
        gbc.gridx = 1;
        add(usernameField, gbc);

        // 密码
        gbc.gridx = 0; gbc.gridy = 1;
        add(new JLabel("密码："), gbc);
        passwordField = new JPasswordField(15);
        gbc.gridx = 1;
        add(passwordField, gbc);

        // 自动填充保存的账号密码
        String[] remembered = AccountMemoryUtil.loadAccount("teacher");
        usernameField.setText(remembered[0]);
        passwordField.setText(remembered[1]);

        // 验证码
        gbc.gridx = 0; gbc.gridy = 2;
        add(new JLabel("验证码："), gbc);
        JPanel captchaPanel = new JPanel(new BorderLayout(5,0));
        captchaField = new JTextField(6);
        captchaLabel = new JLabel();
        captchaLabel.setFont(new Font("Arial", Font.BOLD, 18));
        captchaLabel.setForeground(new Color(255, 105, 180));
        captchaLabel.setHorizontalAlignment(JLabel.CENTER);
        captchaLabel.setPreferredSize(new Dimension(80, 30));
        generateCaptcha();
        captchaPanel.add(captchaField, BorderLayout.CENTER);
        captchaPanel.add(captchaLabel, BorderLayout.EAST);
        gbc.gridx = 1;
        add(captchaPanel, gbc);

        // 登录按钮
        JButton loginBtn = new JButton("登录");
        loginBtn.setBackground(new Color(255, 105, 180));
        loginBtn.setForeground(Color.WHITE);
        loginBtn.setFocusPainted(false);
        loginBtn.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
        gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2;
        add(loginBtn, gbc);

        // 登录逻辑绑定
        loginBtn.addActionListener(e -> login());

        // 点击验证码刷新
        captchaLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                generateCaptcha();
            }
        });

        setVisible(true);
    }

    private void generateCaptcha() {
        generatedCaptcha = String.valueOf((int)((Math.random() * 9 + 1) * 1000));
        captchaLabel.setText(generatedCaptcha);
    }

    private void login() {
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();
        String inputCaptcha = captchaField.getText().trim();

        if (!generatedCaptcha.equals(inputCaptcha)) {
            JOptionPane.showMessageDialog(this, "验证码错误", "错误", JOptionPane.ERROR_MESSAGE);
            generateCaptcha();
            return;
        }

        TeacherDao dao = new TeacherDao();
        Teacher teacher = dao.login(username, password);
        if (teacher != null) {
            // 登录成功保存账号密码
            AccountMemoryUtil.saveAccount(username, password, "teacher");
            JOptionPane.showMessageDialog(this, "欢迎教师：" + teacher.getName());
            this.setVisible(false);
            new TeacherMainFrame(teacher).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "用户名或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
            generateCaptcha();
        }
    }
}
